package com.sx.zongzhi.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.bean.EconomicOrganizionBean;
import com.sx.zongzhi.bean.MsgEvent;
import com.sx.zongzhi.bean.UserPermission;
import com.sx.zongzhi.net.ServiceFactory;
import com.sx.zongzhi.presenter.EconomicOrganizationDetailPest;
import com.sx.zongzhi.utils.BaseUtil;
import com.sx.zongzhi.utils.MyRxBus;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.EconomicOrganizationDetailView;
import com.sx.zongzhi.widget.CustomDialog;
import com.sx.zongzhi.widget.LoadDialog;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * 非公有制经济组织详情
 * Created by admin on 2017/12/8.
 */

public class EconomicOrganizationDetailActivity extends BaseActivity implements EconomicOrganizationDetailView {

    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.imv1)
    ImageView imv1;
    @BindView(R.id.imv_Head)
    ImageView imvHead;
    @BindView(R.id.rl_Head)
    RelativeLayout rlHead;
    @BindView(R.id.et_name)
    TextView etName;
    @BindView(R.id.sp_style)
    TextView spStyle;
    @BindView(R.id.et_residence)
    TextView etResidence;
    @BindView(R.id.et_enterpriseMobile)
    TextView etEnterpriseMobile;
    @BindView(R.id.vi)
    View vi;
    @BindView(R.id.et_enterpriseTelephone)
    TextView etEnterpriseTelephone;
    @BindView(R.id.rbt_Yes)
    RadioButton rbtYes;
    @BindView(R.id.rbt_No)
    RadioButton rbtNo;
    @BindView(R.id.rg_isDangerousEnterprise)
    RadioGroup rgIsDangerousEnterprise;
    @BindView(R.id.et_chief)
    TextView etChief;
    @BindView(R.id.et_mobileNumber)
    TextView etMobileNumber;
    @BindView(R.id.vi1)
    View vi1;
    @BindView(R.id.et_telephone)
    TextView etTelephone;
    @BindView(R.id.sp_chiefCardType)
    TextView spChiefCardType;
    @BindView(R.id.et_cardNo)
    TextView etCardNo;
    @BindView(R.id.et_chiefSafe)
    TextView etChiefSafe;
    @BindView(R.id.et_mobileNumberSafe)
    TextView etMobileNumberSafe;
    @BindView(R.id.vi2)
    View vi2;
    @BindView(R.id.et_telephoneSafe)
    TextView etTelephoneSafe;
    @BindView(R.id.et_licenseNumber)
    TextView etLicenseNumber;
    @BindView(R.id.rbt_Yes1)
    RadioButton rbtYes1;
    @BindView(R.id.rbt_No1)
    RadioButton rbtNo1;
    @BindView(R.id.rg_isHaveOrganizationCondition)
    RadioGroup rgIsHaveOrganizationCondition;
    @BindView(R.id.rbt_Yes2)
    RadioButton rbtYes2;
    @BindView(R.id.rbt_No2)
    RadioButton rbtNo2;
    @BindView(R.id.rg_isHaveOrganization)
    RadioGroup rgIsHaveOrganization;
    @BindView(R.id.rbt_Yes3)
    RadioButton rbtYes3;
    @BindView(R.id.rbt_No3)
    RadioButton rbtNo3;
    @BindView(R.id.rg_isHaveGuild)
    RadioGroup rgIsHaveGuild;
    @BindView(R.id.rbt_Yes4)
    RadioButton rbtYes4;
    @BindView(R.id.rbt_No4)
    RadioButton rbtNo4;
    @BindView(R.id.rg_isHaveGroup)
    RadioGroup rgIsHaveGroup;
    @BindView(R.id.rbt_Yes5)
    RadioButton rbtYes5;
    @BindView(R.id.rbt_No5)
    RadioButton rbtNo5;
    @BindView(R.id.rg_isHaveWomenFederation)
    RadioGroup rgIsHaveWomenFederation;
    @BindView(R.id.et_employeeNumber)
    TextView etEmployeeNumber;
    @BindView(R.id.et_partyMemberNumber)
    TextView etPartyMemberNumber;
    @BindView(R.id.ll_isHaveOrganization)
    RelativeLayout llIsHaveOrganization;
    @BindView(R.id.et_theMemeberOfGuild)
    TextView etTheMemeberOfGuild;
    @BindView(R.id.ll_isHaveGuild)
    RelativeLayout llIsHaveGuild;
    @BindView(R.id.et_theMemeberOfWomenFederation)
    TextView etTheMemeberOfWomenFederation;
    @BindView(R.id.ll_isHaveWomenFederation)
    RelativeLayout llIsHaveWomenFederation;
    @BindView(R.id.et_theMemeberOfGroup)
    TextView etTheMemeberOfGroup;
    @BindView(R.id.ll_isHaveGroup)
    RelativeLayout llIsHaveGroup;
    @BindView(R.id.sp_attentionExtent)
    TextView spAttentionExtent;
    @BindView(R.id.sp_dangerType)
    TextView spDangerType;

    private String id;

    private EconomicOrganizionBean bean;
    private Disposable disposable;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_economicdetail);
        setTitleName("非公有制经济组织详情");
        ButterKnife.bind(this);

        UserPermission userPermission = (UserPermission) getIntent().getSerializableExtra("userPermission");
        if (userPermission != null && userPermission.isDeleteNewEconomicOrganizations()){
            setRightImage(R.mipmap.icon_nav_delete);
        }
        if (userPermission != null && userPermission.isUpdateNewEconomicOrganizations()){
            setRightImage2(R.mipmap.icon_nav_edit);
        }

        id = getIntent().getStringExtra("id");
        LoadDialog.show(EconomicOrganizationDetailActivity.this, "加载中...");
        new EconomicOrganizationDetailPest(this).getEconomicOrganizationDetail(this, id);
        initRx();
    }

    private void initRx() {
        disposable = MyRxBus.get().toObservable(MsgEvent.class).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<MsgEvent>() {
                    @Override
                    public void accept(MsgEvent bean) throws Exception {
                        if (bean.getRequest().equals("EconomicOrganizationDetailActivity")) {
                            LoadDialog.show(EconomicOrganizationDetailActivity.this, "加载中...");
                            new EconomicOrganizationDetailPest(EconomicOrganizationDetailActivity.this)
                                    .getEconomicOrganizationDetail(EconomicOrganizationDetailActivity.this, id);
                        }
                    }
                });
    }

    @OnClick({R.id.imv_Right, R.id.imv_Right2,R.id.imv_Head})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.imv_Right:
                createDelete();
                break;
            case R.id.imv_Right2:
                if (bean == null) {
                    return;
                }
                startActivity(new Intent(this, UpdateEconomicOrganizationActivity.class).putExtra("bean", bean));
                break;
            case R.id.imv_Head:
                if (bean!=null&&!BaseUtil.isEmpty(bean.getImgUrl())){
                    String path=ServiceFactory.NEW_API_BASE_URL+File.separator+bean.getImgUrl();
                    startActivity(new Intent(this,ImagePlayActivity.class)
                    .putExtra("path",path));
                }
                break;
        }
    }

    private void createDelete() {
        CustomDialog.Builder builder = new CustomDialog.Builder(this);
        CustomDialog dialog = builder.setMessage("确定删除此非公有制经济组织吗？").
                setBackButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).setConfirmButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                new EconomicOrganizationDetailPest(EconomicOrganizationDetailActivity.this)
                        .deleteEconomicOrganization(EconomicOrganizationDetailActivity.this, bean.getId());
                dialog.dismiss();
            }
        }).create();
        dialog.show();
    }

    @Override
    public void showError(String message) {
        LoadDialog.dismiss(EconomicOrganizationDetailActivity.this);
        ToastUtils.showToast(EconomicOrganizationDetailActivity.this, message);
    }

    @Override
    public void getEconomicOrganizationDetailSuccess(EconomicOrganizionBean ecBean) {
        LoadDialog.dismiss(EconomicOrganizationDetailActivity.this);
        bean = ecBean;
        if (bean!=null){
            initData();
        }
    }

    private void initData() {
        if (!BaseUtil.isEmpty(bean.getImgUrl())) {
            String path=ServiceFactory.NEW_API_BASE_URL + File.separator + bean.getImgUrl();
            RequestOptions options = new RequestOptions().centerCrop()
                    .placeholder(R.mipmap.img_deful_house)
                    .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                    .error(R.mipmap.img_deful_house);
            Glide.with(this)
                    .load(path)
                    .apply(options)
                    .into(imvHead);
        }
        etName.setText(BaseUtil.isEmpty(bean.getName()) ? "--" : bean.getName());
        spStyle.setText(bean.getStyle() == null ? "--" : bean.getStyle().getDisplayName());
        etResidence.setText(BaseUtil.isEmpty(bean.getResidence()) ? "--" : bean.getResidence());
        etEnterpriseMobile.setText(BaseUtil.isEmpty(bean.getEnterpriseMobile()) ? "--" : bean.getEnterpriseMobile());
        etEnterpriseTelephone.setText(BaseUtil.isEmpty(bean.getEnterpriseTelephone()) ? "--" : bean.getEnterpriseTelephone());
        spChiefCardType.setText(bean.getChiefCardType() == null ? "--" : bean.getChiefCardType().getDisplayName());
        etCardNo.setText(BaseUtil.isEmpty(bean.getCardNo()) ? "--" : bean.getCardNo());
        etChief.setText(BaseUtil.isEmpty(bean.getChief()) ? "--" : bean.getChief());
        etMobileNumber.setText(BaseUtil.isEmpty(bean.getMobileNumber()) ? "--" : bean.getMobileNumber());
        etTelephone.setText(BaseUtil.isEmpty(bean.getTelephone()) ? "--" : bean.getTelephone());
        etChiefSafe.setText(BaseUtil.isEmpty(bean.getChiefSafe()) ? "--" : bean.getChiefSafe());
        etMobileNumberSafe.setText(BaseUtil.isEmpty(bean.getMobileNumberSafe()) ? "--" : bean.getMobileNumberSafe());
        etTelephoneSafe.setText(BaseUtil.isEmpty(bean.getTelephoneSafe()) ? "--" : bean.getTelephoneSafe());
        etLicenseNumber.setText(BaseUtil.isEmpty(bean.getLicenseNumber()) ? "--" : bean.getLicenseNumber());
        spAttentionExtent.setText(bean.getAttentionExtent() == null ? "--" : bean.getAttentionExtent().getDisplayName());
        spDangerType.setText(bean.getDangerType() == null ? "--" : bean.getDangerType().getDisplayName());

        if (bean.isDangerousEnterprise()) {
            rbtYes.setChecked(true);
        } else {
            rbtNo.setChecked(true);
        }
        if (bean.isHaveOrganizationCondition()) {
            rbtYes1.setChecked(true);
        } else {
            rbtNo1.setChecked(true);
        }
        if (bean.isHaveOrganization()) {
            rbtYes2.setChecked(true);
        } else {
            rbtNo2.setChecked(true);
        }
        if (bean.isHaveGuild()) {
            rbtYes3.setChecked(true);
        } else {
            rbtNo3.setChecked(true);
        }
        if (bean.isHaveGroup()) {
            rbtYes4.setChecked(true);
        } else {
            rbtNo4.setChecked(true);
        }
        if (bean.isHaveWomenFederation()) {
            rbtYes5.setChecked(true);
        } else {
            rbtNo5.setChecked(true);
        }

        etEmployeeNumber.setText(bean.getEmployeeNumber() > 0 ? bean.getEmployeeNumber() + "" : "--");
        etPartyMemberNumber.setText(bean.getPartyMemberNumber() > 0 ? bean.getPartyMemberNumber() + "" : "--");
        etTheMemeberOfGuild.setText(bean.getTheMemeberOfGuild() > 0 ? bean.getTheMemeberOfGuild() + "" : "--");
        etTheMemeberOfWomenFederation.setText(bean.getTheMemeberOfWomenFederation() > 0 ? bean.getTheMemeberOfWomenFederation() + "" : "--");
        etTheMemeberOfGroup.setText(bean.getTheMemeberOfGroup() > 0 ? bean.getTheMemeberOfGroup() + "" : "--");

    }

    @Override
    public void deleteEconomicOrganizationDetailSuccess(Boolean bean) {
        LoadDialog.dismiss(EconomicOrganizationDetailActivity.this);
        MyRxBus.get().post(new MsgEvent("EconomicOrganizationActivity", 1, null));
        ToastUtils.showToast(EconomicOrganizationDetailActivity.this, "删除成功");
        finish();
    }

    @Override
    protected void onDestroy() {
        if (disposable != null) {
            disposable.dispose();
        }
        super.onDestroy();
    }
}
